(function () {

	'use strict';

	angular
		.module('app')
		.controller('CartController', CartController);

	CartController.$inject = ['$scope', '$http', '$location', '$state', 'ConfirmCartService', 'CartModel'];

	function CartController($scope, $http, $location, $state, ConfirmCartService, CartModel) {

		$scope.showDialog = false;

		$scope.deletingGoods = null;
        $scope.isSelectedAll = false;
        $scope.selectedGoods = [];
        $scope.selectedAmount = 0;
        $scope.selectedPrice = 0;
        $scope.cartModel = CartModel;

		$scope.touchDelete = function (cart) {
            $scope.deletingGoods = cart;
            $scope.showDialog = true;
        };
		$scope.touchDialogCancel = function () {
            $scope.showDialog = false;
            $scope.deletingGoods = null;
        };
		$scope.touchDialogConfirm = function () {
            if (!$scope.deletingGoods)
                return;

            $scope.cartModel.delete($scope.deletingGoods.cart_id).then(function (res) {
                if (res) {
                    var selectedGoods = $scope.selectedGoods;
                    for (var i = 0; i < selectedGoods.length; ++i) {
                        var cart = selectedGoods[i];
                        if (cart.cart_id == $scope.deletingGoods.cart_id) {
                            selectedGoods.splice(i, 1);
                            break;
                        }
                    }
                    $scope.toast('删除成功');
                    $scope.showDialog = false;
                    $scope.deletingGoods = null;
                    _recomputePrice();
                }
            })
        };

		$scope.touchProduct = function (goods_id) {
            $state.go('goods-info', {
                goods_id: goods_id
            });
        };
		$scope.touchSubmit = function () {
            if (!$scope.selectedGoods || !$scope.selectedGoods.length) {
                $scope.toast('请先选择商品');
                return;
            }

            ConfirmCartService.clear();
            ConfirmCartService.goods = $scope.selectedGoods;
            $state.go('confirm-cart', {});
        };

		$scope.touchAmountSub = function(cart) {
            if (cart.amount <= 1) {
                return;
            }
            $scope.cartModel.update(cart.cart_id, -1).then(function (res) {
                if (res) {
                    // TODO:
                }
                _recomputePrice();
            });
        };

		$scope.touchAmountAdd = function (cart) {
            $scope.cartModel.update(cart.cart_id, 1).then(function (res) {
                if (res) {
                    // TODO:
                }
                _recomputePrice();
            });
        };

		$scope.touchSelect = function (cart) {
            var selectedGoods = $scope.selectedGoods;
            var cartIndex = -1;
            for (var i = 0; i < selectedGoods.length; ++i) {
                var goods = selectedGoods[i];
                if (goods.cart_id == cart.cart_id) {
                    cartIndex = i;
                    break;
                }
            }
            if (-1 == cartIndex) {
                selectedGoods.push(cart);
                if (selectedGoods.length == $scope.cartModel.carts.length) {
                    $scope.isSelectedAll = true;
				}
            } else {
                selectedGoods.splice(cartIndex, 1);
                $scope.isSelectedAll = false;
            }

            _recomputePrice();
        };

		$scope.touchSelectAll = function () {
            var carts = $scope.cartModel.carts;
            var selectedGoods = [];
            var isSelectedAll = $scope.isSelectedAll ? false : true;
            if (isSelectedAll) {
                for (var i = 0; i < carts.length; ++i) {
                    var cart = carts[i];
                    selectedGoods.push(cart);
                }
            }
            $scope.selectedGoods = selectedGoods;
            $scope.isSelectedAll = isSelectedAll;

            _recomputePrice();
        };

		$scope.isSelected = function (cart) {
            var selectedGoods = $scope.selectedGoods;
            for (var i = 0; i < selectedGoods.length; ++i) {
                if (cart.cart_id == selectedGoods[i].cart_id) {
                    return true;
                }
            }
            return false;
        };

		function _recomputePrice() {
			var num = 0;
			var price = 0;
			var goods = $scope.selectedGoods;

			for (var i = 0; i < goods.length; ++i) {
				num += Number(goods[i].num);
				price += goods[i].num * goods[i].price;
			}

			$scope.selectedAmount = num;
			$scope.selectedPrice = price/100;
		}

		function _reload() {
			$scope.cartModel.reload();
		}

		_reload();
	}

})();